package com.larklive.showtime.fragmentmain;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.larklive.showtime.adapter.MyViewPagerAdapter;
import com.larklive.showtime.fragmentsub.FragmentHighPrice;
import com.larklive.showtime.fragmentsub.FragmentLowPrice;
import com.larklive.showtime.fragmentsub.FragmentNewProduct;
import com.larklive.showtime.fragmentsub.FragmentPopularity;
import com.larklive.showtime.fragmentsub.FragmentSecondsOpen;

/**
 * Created by janiszhang on 2016/6/10.
 */

public class TestFragment1 extends android.support.v4.app.Fragment{

    private View viewContent;
    private TabLayout tab_essence;
    private ViewPager vp_essence;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if(viewContent==null){
            viewContent = inflater.inflate(R.layout.fragment_test_1,container,false);
            initConentView(viewContent);
            initData();
        }
        //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) viewContent.getParent();
        if (parent != null) {
            parent.removeView(viewContent);
        }

        return viewContent;
    }

    public void initConentView(View viewContent) {
        this.tab_essence = (TabLayout) viewContent.findViewById(R.id.tab_essence);
        this.vp_essence = (ViewPager) viewContent.findViewById(R.id.vp_essence);
    }

    public void initData() {
        //获取标签数据
        String[] titles = getResources().getStringArray(R.array.home_video_tab);

        //创建一个viewpager的adapter

        ////
        MyViewPagerAdapter viewPagerAdapter = new MyViewPagerAdapter(getFragmentManager());
        viewPagerAdapter.addFragment(FragmentPopularity.newInstance(), "人气");//添加Fragment
        viewPagerAdapter.addFragment(FragmentNewProduct.newInstance(), "新品");
        viewPagerAdapter.addFragment(FragmentSecondsOpen.newInstance(), "秒开");
        viewPagerAdapter.addFragment(FragmentHighPrice.newInstance(), "高价");
        viewPagerAdapter.addFragment(FragmentLowPrice.newInstance(), "低价");
        ////
        this.vp_essence.setAdapter(viewPagerAdapter);

        //将TabLayout和ViewPager关联起来
        this.tab_essence.setupWithViewPager(this.vp_essence);
    }
}
